package cn.sangedon.review.analysis.strategy.specific;

import cn.sangedon.review.analysis.constant.StrategyEnum;
import cn.sangedon.review.common.domain.analysis.StockStrategy;
import cn.sangedon.review.common.domain.analysis.StockStrategyResult;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 主板今天成交量比前10天(包括非交易日)的平均成交量的0.7倍还低, 且今日成交量大于5000w
 */
@Slf4j
@Component
public class TenAverageVolStrategy extends AbstractScheduleTradeStrategy {
    @Override
    public void runJob(List<StockStrategy> strategyList) {
        strategyList.stream().filter(e -> e.getStrategyName().equals(StrategyEnum.TEN_DAY_AVE.getName())).findFirst().ifPresent(e -> {
            addSchedule(e);
        });
    }

    @Override
    public void execute() {
        log.info("开始执行十日平均缩量策略");
        AtomicInteger index = new AtomicInteger(0);
        while (index.getAndIncrement() < 3) {
            try {
                List<StockStrategyResult> results = stockStrategyResultService.getTenAverageVolResult(stockStrategy);
                results.forEach(e -> {
                    e.setStrategyId(stockStrategy.getId());
                    e.setStrategyName(stockStrategy.getStrategyName());
                });
                stockStrategyResultService.saveOrUpdateBatch(results);
                log.info("十日平均缩量策略:成功, size:{}", results.size());

                index.set(3);
            } catch (Exception e) {
                log.error("执行十日平均缩量策略失败:{}", e);
            }
        }
    }
}
